/*
 * SetupGameDialog.java
 */

package othello.gui;

import othello.logic.player.Computer;
import othello.logic.player.Human;
import othello.logic.player.Player;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class SetupGameDialog extends JDialog implements ActionListener {

    private JLabel jlblBlack = new JLabel("Black: ");
    private JLabel jlblWhite = new JLabel("White: ");
    
    private JTextField jtxfBName = new JTextField(20);
    private TextPrompt txpBName = new TextPrompt("Name", jtxfBName);
    private JTextField jtxfWName = new JTextField(20);
    private TextPrompt txpWName = new TextPrompt("Name", jtxfWName);
    
    private JRadioButton jrdbBlackOne = new JRadioButton("Human");
    private JRadioButton jrdbBlackTwo = new JRadioButton("Computer");
    private JRadioButton jrdbWhiteOne = new JRadioButton("Human");
    private JRadioButton jrdbWhiteTwo = new JRadioButton("Computer");
    
    private ButtonGroup bgBlack = new ButtonGroup();
    private ButtonGroup bgWhite = new ButtonGroup();
    
    private FlowLayout flw = new FlowLayout();
    private GridLayout grd = new GridLayout(3,1);
    
    private JPanel jpnlBlack = new JPanel();
    private JPanel jpnlWhite = new JPanel();
    private JPanel jpnlButton = new JPanel();
    private JButton jbtnPlay = new JButton("Play");
    
    private Player player1, player2;
    
    public SetupGameDialog(Frame owner) {
        
        super(owner);
        
        
        setModal(true);
        setTitle("Setup game");
        setResizable(false);
        setLayout(grd);
        
        configureTextPrompt();
        configureRadioButtons();
        configureButton();
        addPanels();
        
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        setSize(300, 185);
        setLocationRelativeTo(owner);
        setVisible(true);
        
    }
    
    //Styles default text in JTextFields
    private void configureTextPrompt() {
        txpBName.changeAlpha(0.5f);
        txpBName.changeStyle(Font.ITALIC);
        txpWName.changeAlpha(0.55f);
        txpWName.changeStyle(Font.ITALIC);
    }
    
    // Configures ButtonGroups and JPanels for JRadioButtons
    private void configureRadioButtons() {
        bgBlack.add(jrdbBlackOne);
        bgBlack.add(jrdbBlackTwo);
        bgWhite.add(jrdbWhiteOne);
        bgWhite.add(jrdbWhiteTwo);
        
        jrdbBlackOne.setActionCommand("Human");
        jrdbBlackTwo.setActionCommand("Computer");
        jrdbBlackTwo.setSelected(true);
        jpnlBlack.setLayout(flw);
        jpnlBlack.add(jlblBlack);
        jpnlBlack.add(jtxfBName);
        jpnlBlack.add(jrdbBlackOne);
        jpnlBlack.add(jrdbBlackTwo);
        
        jrdbWhiteOne.setActionCommand("Human");
        jrdbWhiteTwo.setActionCommand("Computer");
        jrdbWhiteTwo.setSelected(true);
        jpnlWhite.setLayout(flw);
        jpnlWhite.add(jlblWhite);
        jpnlWhite.add(jtxfWName);
        jpnlWhite.add(jrdbWhiteOne);
        jpnlWhite.add(jrdbWhiteTwo);
    }
    
    public Player getPlayer1() {
        return player1;
    }
    
    public Player getPlayer2() {
        return player2;
    }
    
    private void setPlayers() {
        int BLACK = 1;
        int WHITE = 2;
        String pName;
        
        
        // Set default names if none are entered.
        pName = jtxfBName.getText();
        if(pName.equals("")){
            pName = "Player 1";
        }
        
        if(bgBlack.getSelection().getActionCommand().equals("Human")) {
   
            player1 = new Human(pName, BLACK);
            
        }else if(bgBlack.getSelection().getActionCommand().equals("Computer")){
            player1 = new Computer(pName, BLACK);
        }
        
        pName = jtxfWName.getText();
        if(pName.equals("")){
            pName = "Player 2";
        }
        
        if(bgWhite.getSelection().getActionCommand().equals("Human")) {
            player2 = new Human(pName, WHITE);
        
        }else if(bgWhite.getSelection().getActionCommand().equals("Computer")) {
            player2 = new Computer(pName, WHITE);
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        setPlayers();
        dispose();
    }
    
    // Configures Play-button
    private void configureButton() {
        jbtnPlay.addActionListener(this);
        jpnlButton.add(jbtnPlay);
    }
    
    // Adds preconfigured JPanels to grid
    private void addPanels() {
        add(jpnlBlack);
        add(jpnlWhite);
        add(jpnlButton);
    }
}